package com.my.style.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;

import com.my.style.basemodul.R;
import com.my.style.ui.fragment.RecommendFragment;
import com.my.style.ui.fragment.DesignFragment;
import com.my.style.ui.fragment.WardrobeFragment;
import com.my.style.ui.fragment.WorkRoomFragment;
import com.sv.jm.baselibrary.base.BaseActivity;
import com.sv.jm.baselibrary.event.EventAction;

public class TabActivity extends BaseActivity {
    FragmentManager fragmentManager;
    RadioGroup rg;
    Fragment[] fragments = {new RecommendFragment(), new WardrobeFragment(), new DesignFragment(), new WorkRoomFragment()};

    @Override
    public int getContainerLayout() {
        return R.layout.activity_tab;
    }

    @Override
    public void OnCoustomLayoutCreated(View v) {
        //showTop().setTitle("我的风格").setLeftButton(R.drawable.ic_ab_back_holo_light_am);
        fragmentManager = getSupportFragmentManager();
        rg = findView(v, R.id.rg);
        chageFragment(0);
    }

    @Override
    public void setListeners() {
        super.setListeners();
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbt_tab1:
                        chageFragment(0);
                        break;
                    case R.id.rbt_tab2:
                        chageFragment(1);
                        break;
                    case R.id.rbt_tab3:
                        chageFragment(2);
                        break;
                    case R.id.rbt_tab4:
                        chageFragment(3);
                        break;
                }
            }
        });
    }

    private void chageFragment(int index) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (!fragments[index].isAdded()) {
            fragmentTransaction.add(R.id.continer, fragments[index]);
        }
        hideFragment(fragmentTransaction);
        fragmentTransaction.show(fragments[index]);
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        for (Fragment f : fragments) {
            fragmentTransaction.hide(f);
        }
    }

    @Override
    protected void onErrorClick() {

    }

    @Override
    public void leftClick() {

    }

    @Override
    public void rightClick() {

    }

    @Override
    public void otherClick(View v) {

    }


    @Override
    protected void onRefreshData() {

    }

    @Override
    public void onOtherActionReceiver(EventAction eventAction) {

    }
}
